﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EF.Model.Base;
using EF.Model;
using EF.Util.DBUtility;
using EF.Util;

/// <summary>
/// Copyright (C) 2010 峰予信息科技有限公司 
/// 版权所有
///  
/// 文件名：         ArgDAO
/// 文件功能描述：   参数表新建,编辑,查看
/// 　
/// 创建标识：       darkross.zhou20100511 
/// 
/// </summary>
namespace EF.DAL.Base
{
    public class ArgDAO
    {
        /// <summary>
        /// 根据参数类型获取参数
        /// </summary>
        /// <param name="argtype">参数类型</param>
        /// <param name="lang">语言</param>
        /// <returns></returns>
        public DataTable GetArg(string argtype, string lang)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@argTypeMark", SqlDbType.NVarChar, 30),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10)
                                       };

            sqlparams[0].Value = argtype;
            sqlparams[1].Value = lang;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseArgGet", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            return dt;
        }

        /// <summary>
        /// 获取ArgType列表
        /// </summary>
        /// <param name="lang">语言标识</param>
        /// <param name="columns">返回列头</param>
        /// <param name="pageIndex">页面标识</param>
        /// <param name="pageSize">页面记录行数</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="rowsCount">记录总数</param>
        /// <returns></returns>
        public DataTable GetArgTypeList(string lang, string columns, int? pageIndex, int? pageSize, string orderBy, out int rowsCount)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@columns", SqlDbType.NVarChar, 1000),
                                           new SqlParameter("@pageIndex", SqlDbType.Int, 4),
                                           new SqlParameter("@pageSize", SqlDbType.Int, 4),
                                           new SqlParameter("@orderBy", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@rowsCount", SqlDbType.Int, 4)
                                       };

            sqlparams[0].Value = lang;
            sqlparams[1].Value = columns;
            sqlparams[2].Value = pageIndex;
            sqlparams[3].Value = pageSize;
            sqlparams[4].Value = orderBy;
            sqlparams[5].Direction = ParameterDirection.Output;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseArgTypeList", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            rowsCount = (int)sqlparams[5].Value;

            return dt;
        }

        /// <summary>
        /// 获取Arg列表
        /// </summary>
        /// <param name="lang">语言标识</param>
        /// <param name="columns">返回列头</param>
        /// <param name="pageIndex">页面标识</param>
        /// <param name="pageSize">页面记录行数</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="rowsCount">记录总数</param>
        /// <returns></returns>
        public DataTable GetArgList(string lang, string columns, int? pageIndex, int? pageSize, string orderBy, out int rowsCount, string sel_argtype_id)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@columns", SqlDbType.NVarChar, 1000),
                                           new SqlParameter("@pageIndex", SqlDbType.Int, 4),
                                           new SqlParameter("@pageSize", SqlDbType.Int, 4),
                                           new SqlParameter("@orderBy", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@rowsCount", SqlDbType.Int, 4),
                                           new SqlParameter ("@Sel_ArgType_id",SqlDbType.NVarChar,20)
                                       };

            sqlparams[0].Value = lang;
            sqlparams[1].Value = columns;
            sqlparams[2].Value = pageIndex;
            sqlparams[3].Value = pageSize;
            sqlparams[4].Value = orderBy;
            sqlparams[5].Direction = ParameterDirection.Output;
            sqlparams[6].Value = sel_argtype_id;

            SqlDataReader dr = DbHelperSQL.RunProcedure("spBaseArgList", sqlparams);
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();

            rowsCount = (int)sqlparams[5].Value;

            return dt;
        }

        /// <summary>
        /// 参数类型新建
        /// </summary>
        /// <param name="model">参数类型Model</param>
        /// <returns>1,新建成功;2,新建失败,参数标识已存在;0,新建失败,数据库异常.</returns>
        public int Add(ArgTypeModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                         new SqlParameter("@ArgTypeMark", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_cn", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_en", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_ja", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@SortIndex", SqlDbType.Int, 4),
                                         new SqlParameter("@CreateUser", SqlDbType.Int, 4),
                                         new SqlParameter("@result", SqlDbType.Int, 4),
                                         new SqlParameter("@lang",SqlDbType.NVarChar, 10),
                                         new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                     };

            sqlparams[0].Value = model.ArgTypeMark;
            sqlparams[1].Value = model.ArgTypeNameCn;
            sqlparams[2].Value = model.ArgTypeNameEn;
            sqlparams[3].Value = model.ArgTypeNameJa;
            sqlparams[4].Value = model.SortIndex;
            sqlparams[5].Value = model.CreateUser;
            sqlparams[6].Direction = ParameterDirection.Output;
            sqlparams[7].Value = lang;
            sqlparams[8].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseArgTypeAdd", sqlparams);

            msg = sqlparams[8].Value.ToString();

            return (int)sqlparams[6].Value;
        }

        /// <summary>
        /// 获取ArgtypeModel
        /// </summary>
        /// <param name="lang">语言</param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public ArgTypeModel GetModel(string lang, int argtypeId)
        {


            DataTable dt = DbHelperSQL.Query(" select * from dbo.tbBaseArgType where ArgTypeid =" + argtypeId).Tables[0];
            
            ArgTypeModel model = new ArgTypeModel();

            if (dt.Rows.Count == 1)
            {

                model.ArgTypeId = Convert.ToInt32(dt.Rows[0]["ArgTypeId"]);
                model.ArgTypeMark = dt.Rows[0]["ArgTypeMark"].ToString ();
                model.ArgTypeNameCn = dt.Rows [0]["ArgTypeName_cn"].ToString ();
                model.ArgTypeNameEn = dt.Rows [0]["ArgTypeName_en"].ToString ();
                model.ArgTypeNameJa = dt.Rows [0]["ArgTypeName_ja"].ToString ();
                model.CreateTime = Convert.ToDateTime(dt.Rows[0]["CreateTime"]);
                model.CreateUser = Convert.ToInt32(dt.Rows[0]["CreateUser"]);
                model.UpdateTime = Convert.ToDateTime(dt.Rows[0]["UpdateTime"]);
                model.UpdateUser = Convert.ToInt32(dt.Rows[0]["UpdateUser"]);
                model.Status = Convert.ToInt32(dt.Rows[0]["Status"]);
                model.SortIndex = Convert.ToInt32(dt.Rows[0]["SortIndex"]);
 
            }           
              
            return model;
        }

        /// <summary>
        /// ArgType编辑
        /// </summary>
        /// <param name="model">ArgTypeModel</param>
        /// <returns>1,编辑成功;0,编辑失败,数据库异常.</returns>
        public int Edit(ArgTypeModel model, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                         new SqlParameter("@ArgTypeId",SqlDbType.Int ,4),  
                                         new SqlParameter("@ArgTypeMark", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_cn", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_en", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@ArgTypeName_ja", SqlDbType.NVarChar, 50),
                                         new SqlParameter("@SortIndex", SqlDbType.Int, 4),
                                         new SqlParameter("@CreateUser", SqlDbType.Int, 4),
                                         new SqlParameter("@UpdateUser",SqlDbType.Int,4),
                                         new SqlParameter("@result", SqlDbType.Int, 4),
                                         new SqlParameter("@lang",SqlDbType.NVarChar, 10),
                                         new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                       };
            sqlparams[0].Value = model.ArgTypeId;
            sqlparams[1].Value = model.ArgTypeMark;
            sqlparams[2].Value = model.ArgTypeNameCn;
            sqlparams[3].Value = model.ArgTypeNameEn;
            sqlparams[4].Value = model.ArgTypeNameJa;
            sqlparams[5].Value = model.SortIndex;
            sqlparams[6].Value = model.CreateUser;
            sqlparams[7].Value = model.UpdateUser;
            sqlparams[8].Direction = ParameterDirection.Output;
            sqlparams[9].Value = lang;
            sqlparams[10].Direction = ParameterDirection.Output;


            DbHelperSQL.RunProcedure("spBaseArgTypeEdit", sqlparams);

            msg = sqlparams[10].Value.ToString();

            return (int)sqlparams[8].Value;
        }

        /// <summary>
        /// ArgType逻辑删除
        /// </summary>
        /// <param name="userId">被逻辑删除ArgTypeId</param>
        /// <param name="updateUser">执行逻辑删除ArgTypeId</param>
        /// <returns>1,逻辑删除成功;0,逻辑删除失败,数据库异常.</returns>
        public int LogicDel(string selArgTypeIds, int updateUser, string lang, out string msg)
        {
            SqlParameter[] sqlparams = {
                                           new SqlParameter("@argtypeIds", SqlDbType.NVarChar, 100),
                                           new SqlParameter("@updateUser", SqlDbType.Int, 4),
                                           new SqlParameter("@result", SqlDbType.Int, 4),
                                           new SqlParameter("@lang", SqlDbType.NVarChar, 10),
                                           new SqlParameter("@msg", SqlDbType.NVarChar, 300)
                                       };

            sqlparams[0].Value = selArgTypeIds;
            sqlparams[1].Value = updateUser;
            sqlparams[2].Direction = ParameterDirection.Output;
            sqlparams[3].Value = lang;
            sqlparams[4].Direction = ParameterDirection.Output;

            DbHelperSQL.RunProcedure("spBaseArgTypeLogicDel", sqlparams);

            msg = sqlparams[4].Value.ToString();

            return (int)sqlparams[2].Value;
        }
    }
}
